package com.tobeface.wechat.message.handler;

import com.google.common.base.Objects;
import com.tobeface.wechat.message.WeChatMessage;
import com.tobeface.wechat.message.WeChatMessageChannel;
import com.tobeface.wechat.message.WeChatMessagePayloads;
import com.tobeface.wechat.message.WeChatMessageTypes;

/**
 * @author loudyn
 */
public abstract class EventMessageHandlerSupport extends WeChatMessageHandlerSupport {

    @Override
    protected final boolean isMessageTypeSupport(String msgType, WeChatMessage message) {
        if (!Objects.equal(msgType, WeChatMessageTypes.EVENT)) {
            return false;
        }

        String event = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_PAYLOAD);
        return isEventMessageSupport(message, event);
    }

    /**
     * @param message
     * @param event
     * @return
     */
    protected boolean isEventMessageSupport(WeChatMessage message, String event) {
        return true;
    }

    @Override
    protected final void onMessageReceived(WeChatMessageChannel channel, WeChatMessage message) {
        String event = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_PAYLOAD);
        onEventMessageReceived(channel, message, event);
    }

    /**
     * @param channel
     * @param message
     * @param event
     */
    protected abstract void onEventMessageReceived(WeChatMessageChannel channel, WeChatMessage message, String event);
}
